<template>
	<view class="u-wrap">
		<u-navbar title="加油站" is-fixed
			background="url('https://www.yplm888.com//uploads/20220803/7ed305f9a93ba50b049a7725662b18cc.png') no-repeat">
		</u-navbar>
		<my-input @search="search" :value='searchVaule'></my-input>
		<view class="u-menu-wrap">
			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
				<view v-for="(item,index) in tabbar" :key="item.id" class="u-tab-item"
					:class="[current==index ? 'u-tab-item-active' : '']" :data-current="index"
					@tap.stop="swichMenu(item.id, index)">
					<text class="u-line-2" @click="">{{item.name}}</text>
				</view>
			</scroll-view>
			<!-- <block v-for="(item,index) in tabbar" :key="item.id"> -->
			<scroll-view scroll-y class="right-box" @scrolltolower="lowerBottom" refresher-enabled="true"
			:refresher-triggered="triggered" :refresher-threshold="50" @refresherpulling="onPulling"
			@refresherrefresh="onRefresh" @refresherrestore="onRestore" @refresherabort="onAbort">
				<view class="page-view">
					<view class="class-item">
						<!-- <view class="item-title">
								<text>{{item.name}}</text>
							</view> -->
						<view class="item-container">
							<view class="thumb-box" v-for="(item1) in classesList" :key="item1.id"
								@click="toDetail(item1)">
								<!-- <view class="iamge">
									<image class="item-menu-image" :src="item1.cover_image" mode="scaleToFill">
								</view>
								</image> -->
								<view class="right-text">
									<view class="shop-name u-line-3">
										{{item1.name}}
									</view>
									<!-- <view class="shop-price">
										{{item1.money }}
										<text class="org"> 兑换券</text>
									</view> -->
								</view>
								<!-- <view class="item-menu-name margin-top-sm">{{item1.name}}</view> -->
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
			<!-- </block> -->
		</view>
	</view>
</template>

<script>
	import myInput from '@/packageB/pages/components/my-input.vue'
	export default {
		components: {
			myInput
		},
		data() {
			return {
				triggered: false,
				tabbar: [],
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				searchVaule: '',
				baseUrl: this.$u.http.config.baseUrl,
				classesList: [],
				id: '',
				page: 1
			}
		},
		computed: {

		},
		onHide() {
			this.searchVaule = ''
		},
		onLoad(option) {
			this._freshing = false;
			this.getTabber()
		},
		methods: {
			// 获取目录列表
			getTabber() {
				this.$u.api.CultivateTypeLst().then(async res => {
					this.tabbar = res.data
					await this.getClassesList(res.data[0].id)
				})
			},
			async getClassesList(id) {
				this.id = id
				let quaryData = {
					courseclassification_id: id,
					keyword: this.searchVaule,
					page: this.page,
					limit: 15,
				}
				await this.$u.api.CultivateLst(quaryData).then(res => {
					this.classesList = [...this.classesList, ...res.data]
				})
				console.log(this.classesList)
			},
			// 触底加载
			lowerBottom() {
				this.page++
				this.getClassesList(this.id)
			},
			toDetail(item) {
				if (item.type == 0) {
					this.$u.route('/packageB/pages/hostpotDetail/hostpotDetail', {
						id: item.id,
						title: item.name,
						pageType: "jiayouzhan"
					})
				} else {
					this.$u.route('/packageB/pages/videosDetail/videosDetail', {
						id: item.id,
						title: item.name,
						pageType: "jiayouzhan"
					})
				}

			},
			getImg() {
				return Math.floor(Math.random() * 35);
			},
			search(value) {
				console.log(value)
				this.searchVaule = value
				this.classesList = []
				this.getClassesList(this.id)
			},
			// 点击左边的栏目切换
			async swichMenu(id, index) {
				console.log(id, index)
				if (index == this.current) return;
				console.log(2)
				this.current = index;

				// 如果为0，意味着尚未初始化
				if (this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
				this.page = 1
				this.classesList = []
				this.getClassesList(id)
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({
						size: true
					}, res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if (!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return;
						}
						this[dataVal] = res.height;
					}).exec();
				})
			},
			onPulling(e) {
				this.triggered = true;
			},
			async onRefresh() {
				if (this._freshing) return;
				this._freshing = true;
				this.page = 1;
				await setTimeout(() => {
					this.triggered = false;
					this._freshing = false;
					this.dataList = []
					this.initData();
				}, 1000);
				uni.showToast({
					title: '刷新成功',
					duration: 1000
				})
			},
			onRestore() {
				this.triggered = false; // 需要重置
				console.log("onRestore");
			},
			onAbort() {
				console.log("onAbort");
			}
		}
	}
</script>

<style lang="scss" scoped>
	.u-wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
	}

	.u-search-box {
		padding: 18rpx 30rpx;
	}

	.u-menu-wrap {
		margin-top: 10rpx;
		flex: 1;
		display: flex;
		overflow: hidden;
	}

	.u-search-inner {
		background-color: rgb(234, 234, 234);
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		padding: 10rpx 16rpx;
	}

	.u-search-text {
		font-size: 26rpx;
		color: $u-tips-color;
		margin-left: 10rpx;
	}

	.u-tab-view {
		width: 200rpx;
		height: 100%;
	}

	.u-tab-item {
		min-height: 110rpx;
		background: #f6f6f6;
		// box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #444;
		font-weight: 400;
		margin-left: 10rpx;
		// line-height: 1;
	}

	.u-tab-item-active {
		position: relative;
		color: #000;
		font-size: 30rpx;
		font-weight: 600;
		background: #fff;
	}

	.u-tab-item-active::before {
		content: "";
		position: absolute;
		border-left: 4px solid #FE5477;
		min-height: 32rpx;
		left: 0;
		top: 39rpx;
	}

	.u-tab-view {
		height: 100%;
	}

	.right-box {
		background-color: rgb(250, 250, 250);
		height: 100hv;
	}

	.page-view {
		padding: 16rpx;
	}

	.class-item {
		margin-bottom: 30rpx;
		background-color: #fff;
		padding: 16rpx;
		border-radius: 8rpx;
	}

	.item-title {
		font-size: 30rpx;
		color: $u-main-color;
		font-weight: bold;
		margin: 10rpx 0;
	}

	.item-menu-name {
		font-weight: normal;
		font-size: 24rpx;
		color: $u-main-color;
	}

	.item-container {
		display: flex;
		flex-wrap: wrap;
	}

	.thumb-box {
		padding-left: 20rpx;
		width: 100%;
		height: 200rpx;
		display: flex;
		align-items: center;
		margin-top: 10rpx;
		margin-bottom: 10rpx;
		box-shadow: 0px 0px 12px 0px rgba(0, 0, 0, 0.1);
		border-radius: 20rpx;

		.right-text {
			margin: 0 30rpx;

			.shop-name {
				font-size: 26rpx;
				font-weight: 700;
			}

			.shop-price {
				color: #FE5477;
				font-size: 28rpx;

				.org {
					// margin-left: 40rpx;
					font-size: 20rpx;
					// color: #ccc;
					// text-decoration: line-through
				}
			}
		}


	}

	.iamge {
		width: 120rpx;
		height: 120rpx;

		.item-menu-image {
			width: 120rpx;
			height: 120rpx;
			border-radius: 20rpx;
		}
	}
</style>
